<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Bookmark extends CI_Controller {

	public function __construct() {
		parent::__construct();
		
		$this->load->library('hitelesito');
		$this->load->library('template');
		$this->load->library('session');
		$this->load->helper('url');
		
		/*
		Jogosultsagvizsgalat ide...
		*/
		
		if(!$this->hitelesito->is_authenticated()){
			redirect('auth/index');
		}

		if ($this->hitelesito->is_authenticated()) {
			$data = array(
				'belepve'	=> true,
				'azonosito'	=> $this->session->userdata('azonosito'),
				'id'		=> $this->session->userdata('id'),
			);
		}
		else {
			$data = array(
				'belepve'	=> false,
			);
		}
		$this->load->vars($data);
	}

	public function index()
	{
		$this->load->helper('url');
		$this->load->model('bookmark_model');
		$bookmarks = $this->bookmark_model->get_bookmarks();
		
		/*
		$this->load->view('template_head');
		$this->load->view('bookmark_list', array(
			'bookmarks'   => $bookmarks,
		));*/

		$this->template->set_data('js', array(
			'bookmark'
		));
		$this->template->display('bookmark_list', array(
			'bookmarks' => $bookmarks
		));
		//$this->load->view('template_foot');
	}
	
	public function insert()
	{
		$this->load->helper('url');
		$this->load->helper('form');
		$this->load->library('form_validation');
		
		$this->form_validation->set_rules('url', 'URL', 'trim|prep_url|required');
		$this->form_validation->set_rules('desc', 'Description', 'trim');
		
		if ($this->form_validation->run() === false) {
			//$this->load->view('bookmark_insert');
			$this->template->display('bookmark_insert');
		} else {
			$this->load->model('bookmark_model');
			echo $url = $this->input->post('url');
			$desc = htmlspecialchars($this->input->post('desc'));
			$this->bookmark_model->insert($url, $desc);
			redirect('bookmark/index');
		}
	}
	
	
	public function delete($bid){
		$this->load->model('bookmark_model');
		$this->load->library('request');
		//$eredmeny = $this->bookmark_model->delete($bid);
		$ered = true;
		
		if($this->request->is_ajax()){
			$this->load->view('ajax/delete', array(
				'success' => $ered,
			) );
		}
		else{
			redirect('bookmark/index');
		}
		
		//redirect('bookmark/index');
	}

}

//codeigniter/index.php/bookmark/index
